
//添加收货地址


var area = require('../../utils/city.js')

var areaInfo = [];//所有省市区县数据

var provinces = [];//省

var citys = [];//城市

var countys = [];//区县

var index = [0, 0, 0];

var cellId;

var t = 0;
var show = false;
var moveY = 200;

const app = getApp(); //获取全局app.js

Page({
    //页面的初始数据
    data: {
      show: show,
      provinces: provinces,
      citys: citys,
      countys: countys,
      value: [0, 0, 0],
      code: [0, 0, 0],

      province: '',
      city: '',
      county: '',
      id: '',
      addr_name: '',
      phone: '',
      xiangxi: '',
      isfag:''
    },

  //滑动事件
  bindChange: function (e) {
    var val = e.detail.value
    
    //判断滑动的是第几个column
    //若省份column做了滑动则定位到地级市和区县第一位
    if (index[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
      getCityArr(val[0], this);//获取地级市数据
      getCountyInfo(val[0], val[1], this);//获取区县数据
    } else {    //若省份column未做滑动，地级市做了滑动则定位区县第一位
      if (index[1] != val[1]) {
        val[2] = 0;
        getCountyInfo(val[0], val[1], this);//获取区县数据
      }
    }
    index = val;

    console.log(index + " => " + val);
    
    //更新数据
    this.setData({
      value: [val[0], val[1], val[2]],
      province: provinces[val[0]].name,
      city: citys[val[1]].name,
      county: countys[val[2]].name,
      code: [provinces[val[0]].code, citys[val[1]].code, countys[val[2]].code]
    })
    
  },
  onLoad: function (options) {
    cellId = options.cellId;
    var that = this;
    //获取省市区县数据
    area.getAreaInfo(function (arr) {
      areaInfo = arr;
      //获取省份数据
      getProvinceData(that);
    });
    that.setData({
      code: [110000, 110100, 110101]
    })
  },

  //获取姓名
  getName: function (e) {
    let name = e.detail.value;
    this.setData({
      addr_name: name
    });
  },


  //获取手机号
  getMobile: function (e) {
    let mobile = e.detail.value;
    this.setData({
      phone: mobile
    });
  },


  //获取详细地址
  getAddress: function (e) {
    let address = e.detail.value;
    this.setData({
      xiangxi: address
    });
  },
  isDefault:function(e){
    let isfag =2;
    if (e.detail.value == true){
      isfag = 1;
    }
    this.setData({
      isfag: isfag
    });
  },
  
  addAddress: function () {
    var that = this;

    wx.request({
      url: 'https://wukongweixiu.cn/index/Defaults/addrAdd',
      data: {
        uinionid: getApp().globalData.userid,
        isfag:that.data.isfag,
        sheng: that.data.code[0],
        shi: that.data.code[1],
        xian: that.data.code[2],
        xiangxi: that.data.xiangxi,
        addr_name: that.data.addr_name,
        phone: that.data.phone
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        if(res.data.code == 1){
          wx.navigateBack({
          delta: 1
        })
        }else{
          wx.showModal({
            title: '',
            content: res.data.msg,
            showCancel: false
          });
        }

        

      }
    })
    console.log(this.data.code)
  },
  // ------------------- 分割线 --------------------
  onReady: function () {
    this.animation = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 0,
      timingFunction: "ease",
      delay: 0
    }
    )
    this.animation.translateY(200 + 'vh').step();
    this.setData({
      animation: this.animation.export(),
      show: show
    })
  },
  //移动按钮点击事件
  translate: function (e) {
    if (t == 0) {
      moveY = 0;
      show = false;
      t = 1;
    } else {
      moveY = 200;
      show = true;
      t = 0;
    }
    // this.animation.translate(arr[0], arr[1]).step();
    animationEvents(this, moveY, show);

  },
  //隐藏弹窗浮层
  hiddenFloatView(e) {
    console.log(e);
    moveY = 200;
    show = true;
    t = 0;
    animationEvents(this, moveY, show);

  },
  //页面滑至底部事件
  onReachBottom: function () {
    // Do something when page reach bottom.
  }

})

//动画事件
function animationEvents(that, moveY, show) {
  console.log("moveY:" + moveY + "\nshow:" + show);
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: 400,
    timingFunction: "ease",
    delay: 0
  }
  )
  that.animation.translateY(moveY + 'vh').step()

  that.setData({
    animation: that.animation.export(),
    show: show
  })

}


// ---------------- 分割线 ---------------- 

//获取省份数据
function getProvinceData(that) {
  var s;
  provinces = [];
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    s = areaInfo[i];
    if (s.di == "00" && s.xian == "00") {
      provinces[num] = s;
      num++;
    }
  }
  that.setData({
    provinces: provinces
  })

  //初始化调一次
  getCityArr(0, that);
  getCountyInfo(0, 0, that);
  that.setData({
    province: "北京市",
    city: "市辖区",
    county: "东城区",
  })

}

// 获取地级市数据
function getCityArr(count, that) {
  var c;
  citys = [];
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    c = areaInfo[i];
    if (c.xian == "00" && c.sheng == provinces[count].sheng && c.di != "00") {
      citys[num] = c;
      num++;
    }
  }
  if (citys.length == 0) {
    citys[0] = { name: '' };
  }

  that.setData({
    city: "",
    citys: citys,
    value: [count, 0, 0]
  })
}

// 获取区县数据
function getCountyInfo(column0, column1, that) {
  var c;
  countys = [];
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    c = areaInfo[i];
    if (c.xian != "00" && c.sheng == provinces[column0].sheng && c.di == citys[column1].di) {
      countys[num] = c;
      num++;
    }
  }
  if (countys.length == 0) {
    countys[0] = { name: '' };
  }
  that.setData({
    county: "",
    countys: countys,
    value: [column0, column1, 0]
  })
}


  